home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / Unused / HTAuth.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  5.9 KB  |  220 lines

  1.  
  2. /* MODULE                            HTAuth.c
  3. **            USER AUTHENTICATION
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **
  8. ** HISTORY:
  9. **
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15.  
  16. #include <string.h>
  17.  
  18. #include "HTUtils.h"
  19. #include "HTPasswd.h"    /* Password file routines    */
  20. #include "HTAssoc.h"
  21. #include "HTAuth.h"    /* Implemented here        */
  22. #include "HTUU.h"    /* Uuencoding and uudecoding    */
  23.  
  24.  
  25. /* PRIVATE                        decompose_auth_string()
  26. **        DECOMPOSE AUTHENTICATION STRING
  27. **        FOR BASIC OR PUBKEY SCHEME
  28. ** ON ENTRY:
  29. **    authstring    is the authorization string received
  30. **            from browser.
  31. **
  32. ** ON EXIT:
  33. **    returns        a node representing the user information
  34. **            (as always, this is automatically freed
  35. **            by AA package).
  36. */
  37. PRIVATE HTAAUser *decompose_auth_string ARGS2(char *,        authstring,
  38.                           HTAAScheme,    scheme)
  39. {
  40.     static HTAAUser *user = NULL;
  41.     static char *cleartext = NULL;
  42.     char *username = NULL;
  43.     char *password = NULL;
  44.     char *inet_addr = NULL;
  45.     char *timestamp = NULL;
  46.     char *browsers_key = NULL;
  47.     char *extras = NULL;
  48.  
  49.     if (!user && !(user = (HTAAUser*)malloc(sizeof(HTAAUser))))    /* Allocated */
  50.     outofmem(__FILE__, "decompose_auth_string");        /* only once */
  51.  
  52.     user->scheme = scheme;
  53.     user->username = NULL;    /* Not freed, because freeing */
  54.     user->password = NULL;    /* cleartext also frees these */
  55.     user->inet_addr = NULL;    /* See below: ||              */
  56.     user->timestamp = NULL;    /*            ||              */
  57.     user->secret_key = NULL;    /*            ||              */
  58.                                 /*            \/              */
  59.     FREE(cleartext);    /* From previous call.                */
  60.                         /* NOTE: parts of this memory are pointed to by    */
  61.                         /* pointers in HTAAUser structure. Therefore,    */
  62.                         /* this also frees all the strings pointed to    */
  63.             /* by the static 'user'.            */
  64.  
  65.     if (!authstring || !*authstring || 
  66.     scheme != HTAA_BASIC || scheme == HTAA_PUBKEY)
  67.     return NULL;
  68.  
  69.     if (scheme == HTAA_PUBKEY) {    /* Decrypt authentication string */
  70.     int bytes_decoded;
  71.     char *ciphertext;
  72.     int len = strlen(authstring) + 1;
  73.  
  74.     if (!(ciphertext = (char*)malloc(len)) ||
  75.         !(cleartext  = (char*)malloc(len)))
  76.         outofmem(__FILE__, "decompose_auth_string");
  77.  
  78.     bytes_decoded = HTUU_decode(authstring, ciphertext, len);
  79.     ciphertext[bytes_decoded] = (char)0;
  80. #ifdef PUBKEY
  81.     HTPK_decrypt(ciphertext, cleartext, private_key);
  82. #endif
  83.     free(ciphertext);
  84.     }
  85.     else {   /* Just uudecode */
  86.     int bytes_decoded;
  87.     int len = strlen(authstring) + 1;
  88.     
  89.     if (!(cleartext = (char*)malloc(len)))
  90.         outofmem(__FILE__, "decompose_auth_string");
  91.     bytes_decoded = HTUU_decode(authstring, cleartext, len);
  92.     cleartext[bytes_decoded] = (char)0;
  93.     }
  94.  
  95.  
  96. /*
  97. ** Extract username and password (for both schemes)
  98. */
  99.     username = cleartext;
  100.     if (!(password = strchr(cleartext, ':'))) {
  101.     if (TRACE)
  102.         fprintf(stderr, "%s %s\n",
  103.             "decompose_auth_string: password field",
  104.             "missing in authentication string.\n");
  105.     return NULL;
  106.     }
  107.     *(password++) = '\0';
  108.  
  109. /*
  110. ** Extract rest of the fields
  111. */
  112.     if (scheme == HTAA_PUBKEY) {
  113.     if (                          !(inet_addr   =strchr(password, ':')) || 
  114.         (*(inet_addr++)   ='\0'), !(timestamp   =strchr(inet_addr,':')) ||
  115.         (*(timestamp++)   ='\0'), !(browsers_key=strchr(timestamp,':')) ||
  116.         (*(browsers_key++)='\0')) {
  117.  
  118.         if (TRACE) fprintf(stderr, "%s %s\n",
  119.                    "decompose_auth_string: Pubkey scheme",
  120.                    "fields missing in authentication string");
  121.         return NULL;
  122.     }
  123.     extras = strchr(browsers_key, ':');
  124.     }
  125.     else extras = strchr(password, ':');
  126.  
  127.     if (extras) {
  128.     *(extras++) = '\0';
  129.     if (TRACE) fprintf(stderr, "%s `%s' %s `%s'\n",
  130.                "decompose_auth_string: extra field(s) in",
  131.                (scheme==HTAA_BASIC ? "Basic" : "Pubkey"),
  132.                "authorization string ignored:", extras);
  133.     }
  134.  
  135. /*
  136. ** Set the fields into the result
  137. */
  138.     user->username   = username;
  139.     user->password   = password;
  140.     user->inet_addr  = inet_addr;
  141.     user->timestamp  = timestamp;
  142.     user->secret_key = browsers_key;
  143.  
  144.     if (TRACE) {
  145.     if (scheme==HTAA_BASIC)
  146.         fprintf(stderr, "decompose_auth_string: %s (%s,%s)\n",
  147.             "Basic scheme authentication string:",
  148.             username, password);
  149.     else
  150.         fprintf(stderr, "decompose_auth_string: %s (%s,%s,%s,%s,%s)\n",
  151.             "Pubkey scheme authentication string:",
  152.             username, password, inet_addr, timestamp, browsers_key);
  153.     }
  154.     
  155.     return user;
  156. }
  157.  
  158.  
  159.  
  160. PRIVATE BOOL HTAA_checkTimeStamp ARGS1(CONST char *, timestamp)
  161. {
  162.     return NO;        /* This is just a stub */
  163. }
  164.  
  165.  
  166. PRIVATE BOOL HTAA_checkInetAddress ARGS1(CONST char *, inet_addr)
  167. {
  168.     return NO;        /* This is just a stub */
  169. }
  170.  
  171.  
  172. /* SERVER PUBLIC                    HTAA_authenticate()
  173. **            AUTHENTICATE USER
  174. ** ON ENTRY:
  175. **    scheme        used authentication scheme.
  176. **    scheme_specifics the scheme specific parameters
  177. **            (authentication string for Basic and
  178. **            Pubkey schemes).
  179. **    prot        is the protection information structure
  180. **            for the file.
  181. **
  182. ** ON EXIT:
  183. **    returns        NULL, if authentication failed.
  184. **            Otherwise a pointer to a structure
  185. **            representing authenticated user,
  186. **            which should not be freed.
  187. */
  188. PUBLIC HTAAUser *HTAA_authenticate ARGS3(HTAAScheme,    scheme,
  189.                      char *,    scheme_specifics,
  190.                      HTAAProt *,    prot)
  191. {
  192.     if (HTAA_UNKNOWN == scheme || !prot ||
  193.     -1 == HTList_indexOf(prot->valid_schemes, (void*)scheme))
  194.     return NULL;
  195.  
  196.     switch (scheme) {
  197.       case HTAA_BASIC:
  198.       case HTAA_PUBKEY:
  199.     {
  200.         HTAAUser *user = decompose_auth_string(scheme_specifics, scheme);
  201.                                        /* Remember, user is auto-freed */
  202.         if (user &&
  203.         HTAA_checkPassword(user->username,
  204.                    user->password,
  205.                    HTAssocList_lookup(prot->values, "passw")) &&
  206.         (HTAA_BASIC == scheme ||
  207.          (HTAA_checkTimeStamp(user->timestamp) &&
  208.           HTAA_checkInetAddress(user->inet_addr))))
  209.         return user;
  210.         else
  211.         return NULL;
  212.     }
  213.     break;
  214.       default:
  215.     /* Other authentication routines go here */
  216.     return NULL;
  217.     }
  218. }
  219.  
  220.